import { Login } from "@/api/user"
import storage from 'store'
import router from "@/router/router";
const user = {
    state: {
        info: storage.get('info') || {},
        token: storage.get("token") || ''

    },
    mutations:{
        SET_INfO:(state:any,info:any)=>{
            storage.set('info',info)
            state.info = info
        },
        SET_TOKEN:(state:any,token:string)=>{
            storage.set('token',token)
            state.info = token
        }
    },
    actions:{
        Login({ commit }:any,userInfo:any){
            return new Promise((resolve, reject)=>{
                Login(userInfo).then(res=>{
                    commit('SET_TOKEN',res.data.token)
                    commit('SET_INfO',res.data.info)
                    resolve(res)
                }).catch(error=>{
                    reject(error)
                })
            })
        },
        LoginOut({commit}:any){
            commit('SET_TOKEN','')
            commit('SET_INfO',{})
            router.push({
                path:"/userLogin"
            })
        }
    }
}
export default user
